package com.algorithm;

/**
 * 环形链表
 */
public class LinkedListCycle {

    public static boolean hasCycle(ListNode head) {
        if (head==null){
            return false;
        }
        ListNode slow=head;
        ListNode fast=head;
        while (fast.next!=null&&fast.next.next!=null){
            slow=slow.next;
            fast=fast.next.next;
            if (slow==fast){
                return true;
            }
        }
        return false;
    }

    public static void main(String[] args) {
        ListNode head=new ListNode(3,new ListNode(4,new ListNode(0,new ListNode(-4))));
        boolean result = hasCycle(head);
        System.out.println(result);
    }
}
